﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Extensil.Common.CategoryHandler.Discovery
{
    public class Probe :ConfigurationElementCollection
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return base["name"] as string;
            }
            set
            {
                base["name"] = value;
            }
        }
        public bool IsActive
        {
            get
            {
                return (Active.ToLower() == "yes" ||
                    Active.ToLower() == "y" ||
                    Active.ToLower() == "true" ||
                    Active.ToLower() == "on"
                    );
            }
            set
            {
                this.Active = value ? "yes" : "no";
            }
        }
        [ConfigurationProperty("active", IsRequired = false)]
        public string Active
        {
            get
            {
                if(String.IsNullOrEmpty((string)base["active"]))
                    return "yes";
                return base["active"] as String;
            }
            set
            {
                base["active"] = value;
            }
        }
        [ConfigurationProperty("handler", IsRequired = true)]
        public string Handler
        {
            get
            {
                return base["handler"] as string;
            }
            set
            {
                base["handler"] = value;
            }
        }
        public ProbeParam this[int index]
        {
            get
            {
                return base.BaseGet(index) as ProbeParam;
            }
            set
            {
                if(base.Count > index && base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }
        public ProbeParam this[string key]
        {
            get
            {
                return base.BaseGet(key) as ProbeParam;
            }
        }

        public override bool IsReadOnly()
        {
            return false;
        }

        internal void Add(ProbeParam par)
        {
            this.BaseAdd(par);
        }
        public void AddRange(List<ProbeParam> pars)
        {
            foreach(ProbeParam par in pars)
            {
                this.BaseAdd(par);
            }
        }
        internal void Remove(ProbeParam par)
        {
            this.BaseRemove(par.Name);
        }
        internal int IndexOf(ProbeParam par)
        {
            return base.BaseIndexOf(par);
        }
        public void Clear()
        {
            base.BaseClear();
        }

        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return base.CreateNewElement(elementName);
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new ProbeParam();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProbeParam)element).Name;
        }


        protected override string ElementName
        {
            get
            {
                return "param";
            }
        }
        protected override bool IsElementName(string elementName)
        {
            return !String.IsNullOrEmpty(elementName) && elementName.ToLower() == "param";
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
    }
}
